package com.xinfang.superminer.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.xinfang.superminer.R;

/**
 * Created by Administrator on 2018/3/29.
 */

public class MyHeadListView extends ListView implements AbsListView.OnScrollListener {

    private View header;//头布局

    private int headerHeight;//头布局自身的高度

    private int scrollState;//当前滚动状态
    private int firstVisibleItem;//当前可见的第一个item
    private int startY;//刚开始触摸屏幕时的Y值

    private boolean canPull = false;//是否可以执行下拉操作
    private refresfListener mListener;//回调接口

    private TextView tv_uppage;//上一页
    private TextView tv_firstpage;//首页
    public MyHeadListView(Context context) {
        super(context);
        initView(context);
    }

    public MyHeadListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public MyHeadListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void initView(Context context) {

        header = LayoutInflater.from(context).inflate(R.layout.header, null);
        notifyView(header);
        headerHeight = header.getMeasuredHeight();//获取header的高度

        //        headerHeight = header.getHeight();
        paddingTop(-headerHeight);
        //将头布局加进去
        this.addHeaderView(header);

        this.setOnScrollListener(this);

        tv_uppage=header.findViewById(R.id.tv_uppage);
        tv_firstpage=header.findViewById(R.id.tv_firstpage);
        tv_uppage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                paddingTop(-headerHeight);
                //上一页
                mListener.refresh(0);

            }
        });
        tv_firstpage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                paddingTop(-headerHeight);
                //首页
                mListener.refresh(1);

            }
        });
    }

    //定义回调接口
    public interface refresfListener {
        //page 1返回到首页 0返回到上一页
        void refresh(int page);
    }

    /**
     * 修改页码
     * @param listener
     */
    public void setOnUpdateListener(refresfListener listener) {
        this.mListener = listener;
    }
    /**
     * 该方法为通知父布局，子布局view的宽度和高度
     *
     * @param view:子布局
     */
    private void notifyView(View view) {

        ViewGroup.LayoutParams p = view.getLayoutParams();

        if (p == null) {
            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        }

        //spec表示当前子view左右边距,padding表示子view的左右内边距
        //childDimension：子view的宽度
        int width = ViewGroup.getChildMeasureSpec(0, 0, p.width);

        int height;
        int tempHeight = p.height;
        if (tempHeight > 0) {
            //子布局高度不为空，需要填充这个布局
            height = MeasureSpec.makeMeasureSpec(tempHeight, MeasureSpec.EXACTLY);
        } else {
            //高度为0，则不需要填充
            height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        }

        //然后告诉父布局，子布局的高度和宽度
        view.measure(width, height);
    }


    //该方法设定header的paddingTop
    private void paddingTop(int pt) {
            header.setPadding(header.getPaddingLeft(), pt, header.getPaddingRight(), header.getPaddingBottom());
            header.invalidate();
    }
    /***
     * 监听当前滚动状态
     * i：当前滚动状态
     */
    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {
        //记录当前的滚动状态
        scrollState = i;
    }

    /***
     * 监听当前滚动的item
     * i：当前可见的第一个item
     * i1：当前共有多少个item可见
     * i2:总共有多少个item
     */
    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {
        firstVisibleItem = i;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            //手指落到屏幕上时
            case MotionEvent.ACTION_DOWN:
                //如果当前可见的第一个item为第0号，说明ListView位于顶端，可以执行下拉刷新
                if (firstVisibleItem == 0) {
                    canPull = true;
                    startY = (int) ev.getY();
                }

                break;
            //手指在屏幕上拖动时
            case MotionEvent.ACTION_MOVE:
                int tempY = (int) ev.getY();
                int space = tempY - startY;//移动的距离
                if (space > headerHeight  && scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                    paddingTop(0);
                }else
                {
                    int topdding = space - headerHeight;
                    paddingTop(topdding);//即时设定头布局的隐藏高度
                }
                break;
            //手指离开屏幕时
            case MotionEvent.ACTION_UP:
                canPull = false;

                break;
        }
        return super.onTouchEvent(ev);
    }
}
